// AccountManagementDlg.cpp : implementation file
//

#include "stdafx.h"
#include "AccountManagementDlg.h"


// CAccountManagementDlg dialog

IMPLEMENT_DYNAMIC(CAccountManagementDlg, CDialog)

CAccountManagementDlg::CAccountManagementDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CAccountManagementDlg::IDD, pParent)
{

}
CAccountManagementDlg::CAccountManagementDlg(CSecurityProjectDlg* secDlg)
	: CDialog(CAccountManagementDlg::IDD, NULL)
{
	this->secDlg = secDlg;
}

CAccountManagementDlg::~CAccountManagementDlg()
{
}

void CAccountManagementDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST_ACC, m_lstAcc);
}


BEGIN_MESSAGE_MAP(CAccountManagementDlg, CDialog)
	ON_BN_CLICKED(IDCANCEL, &CAccountManagementDlg::OnBnClickedCancel)
	ON_NOTIFY(NM_CLICK, IDC_LIST_ACC, &CAccountManagementDlg::OnNMClickListAcc)
	ON_BN_CLICKED(IDOK, &CAccountManagementDlg::OnBnClickedOk)
	ON_BN_CLICKED(IDC_BUTTON_MANAGE_CREATE, &CAccountManagementDlg::OnBnClickedButtonManageCreate)
	//ON_BN_CLICKED(IDC_BUTTON_MANAGE_CREATE2, &CAccountManagementDlg::OnBnClickedButtonManageCreate2)
	ON_BN_CLICKED(IDC_BUTTON_MANAGE_UPDATE, &CAccountManagementDlg::OnBnClickedButtonManageUpdate)
	ON_BN_CLICKED(IDC_BUTTON_MANAGE_DEL, &CAccountManagementDlg::OnBnClickedButtonManageDel)
END_MESSAGE_MAP()


// CAccountManagementDlg message handlers

void CAccountManagementDlg::OnBnClickedCancel()
{
	// TODO: Add your control notification handler code here
	OnCancel();
}

void CAccountManagementDlg::OnNMClickListAcc(NMHDR *pNMHDR, LRESULT *pResult)
{
	NM_LISTVIEW* pNM = (NM_LISTVIEW*)pNMHDR;
	int nCount = m_lstAcc.GetItemCount();
    LVHITTESTINFO ht;
    ht.pt = pNM->ptAction;
    m_lstAcc.SubItemHitTest(&ht);

	if(ht.iItem >= 0 && ht.iItem < nCount)
	{
		accClick = new Account();
		
		accClick->m_StrUserName = m_lstAcc.GetItemText(ht.iItem, 1);
		accClick->m_StrDescription = m_lstAcc.GetItemText(ht.iItem, 2);
		
		SetDlgItemText(IDC_EDIT_MANAGE_NAME,accClick->m_StrUserName);
		SetDlgItemText(IDC_EDIT_MANAGE_DES,accClick->m_StrDescription );
	}
	else
	{
		accClick = NULL;
	}
}

void CAccountManagementDlg::AddAccountToList(CString username, CString description)
{
	int index = m_lstAcc.GetItemCount();
	m_lstAcc.InsertItem(0, NULL);
	m_lstAcc.SetItemText(0, 0, secDlg->ToCString(index + 1));
	m_lstAcc.SetItemText(0, 1, username);
	m_lstAcc.SetItemText(0, 2, description);
	UpdateData(FALSE);

}

void CAccountManagementDlg::InitListControl()
{
	ListView_SetExtendedListViewStyle(m_lstAcc, LVS_EX_GRIDLINES|LVS_EX_FULLROWSELECT);	
	m_lstAcc.InsertColumn(0, _T("STT"), LVCFMT_LEFT, 35);
	m_lstAcc.InsertColumn(1, _T("Username"), LVCFMT_LEFT, 110);
	m_lstAcc.InsertColumn(2, _T("Description"), LVCFMT_LEFT, 50);


}
void CAccountManagementDlg::LoadAccountList()
{
	m_lstAcc.DeleteAllItems();
	secDlg->SendMessageToServer(_T("A7"));
}

BOOL CAccountManagementDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	InitListControl();
	LoadAccountList();
	

	return TRUE;  
}
void CAccountManagementDlg::OnBnClickedOk()
{
	// TODO: Add your control notification handler code here
	OnOK();
}

void CAccountManagementDlg::OnBnClickedButtonManageCreate()
{
	// TODO: Add your control notification handler code here
	CString username;
	CString password;
	CString description;
	GetDlgItemText(IDC_EDIT_MANAGE_NAME,username);
	GetDlgItemText(IDC_EDIT_MANAGE_PASS,password);
	GetDlgItemText(IDC_EDIT_MANAGE_DES,description);
	if(username != _T("") && password != _T(""))
	{
		//Message de create account co cau truc :  AXusernameYpasswordZDescription 
		// X,Y,Z la kick thuoc username,pass,description (co dinh thieu dai X,Y,Z la 2 )
		CString message = _T("A0");
		if(username.GetLength() < 10)
			message += _T("0");
		message += secDlg->ToCString(username.GetLength()) + username;
		if(password.GetLength() < 10)
			message += _T("0");
		message += secDlg->ToCString(password.GetLength()) + password;
		if(description.GetLength() < 10)
			message += _T("0");
		message += secDlg->ToCString(description.GetLength()) + description;

		secDlg->SendMessageToServer(message);
	}
	else
	{
		MessageBox(_T("Phai nhap username va password"));
	}
}

void CAccountManagementDlg::OnBnClickedButtonManageCreate2()
{
	// TODO: Add your control notification handler code here
	CString username;
	CString description;
	GetDlgItemText(IDC_EDIT_MANAGE_NAME,username);
	GetDlgItemText(IDC_EDIT_MANAGE_DES,description);
	if(username != _T(""))
	{
		//Message de create account co cau truc :  AXusernameYpasswordZDescription 
		// X,Y,Z la kick thuoc username,pass,description (co dinh thieu dai X,Y,Z la 2 )
		CString message = _T("A9");
		if(username.GetLength() < 10)
			message += _T("0");
		message += secDlg->ToCString(username.GetLength()) + username;

		if(description.GetLength() < 10)
			message += _T("0");
		message += secDlg->ToCString(description.GetLength()) + description;

		secDlg->SendMessageToServer(message);
	}
	else
	{
		MessageBox(_T("Phai nhap username"));
	}

}

void CAccountManagementDlg::OnBnClickedButtonManageUpdate()
{
	// TODO: Add your control notification handler code here
	CString username;
	CString description;
	GetDlgItemText(IDC_EDIT_MANAGE_NAME,username);
	GetDlgItemText(IDC_EDIT_MANAGE_DES,description);
	if(username != _T(""))
	{
		//Message de create account co cau truc :  AXusernameYpasswordZDescription 
		// X,Y,Z la kick thuoc username,pass,description (co dinh thieu dai X,Y,Z la 2 )
		CString message = _T("A9");
		if(username.GetLength() < 10)
			message += _T("0");
		message += secDlg->ToCString(username.GetLength()) + username;

		if(description.GetLength() < 10)
			message += _T("0");
		message += secDlg->ToCString(description.GetLength()) + description;

		secDlg->SendMessageToServer(message);
	}
	else
	{
		MessageBox(_T("Phai nhap username"));
	}

	
}

void CAccountManagementDlg::OnBnClickedButtonManageDel()
{
	// TODO: Add your control notification handler code here
	CString username;	
	GetDlgItemText(IDC_EDIT_MANAGE_NAME,username);
	if(username != _T(""))
	{
		//Message de create account co cau truc :  AXusernameYpasswordZDescription 
		// X,Y,Z la kick thuoc username,pass,description (co dinh thieu dai X,Y,Z la 2 )
		CString message = _T("AC");
		message += username;
		secDlg->SendMessageToServer(message);
	}
	else
	{
		MessageBox(_T("Phai nhap username"));
	}
}
